package com.konne.nightmare.FastPublicOpinion.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CompoundButton;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.blankj.utilcode.util.SizeUtils;
import com.blankj.utilcode.util.StringUtils;
import com.konne.nightmare.FastPublicOpinion.R;

/**
 * Create by ws.
 * on Data:2022/2/10  Time:下午 3:51.
 * description: ** .
 */
public class QJRadioButton extends LinearLayout implements QJRadio{
    private ImageView iv;
    private TextView tv;
    private boolean isSel;
    private boolean qjrb_enabled;
    private OnCheckedChangeListener onCheckedChangeListener;
    public QJRadioButton(@NonNull Context context) {
        this(context, null);
    }

    public QJRadioButton(@NonNull Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public QJRadioButton(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        setOrientation(HORIZONTAL);
        setGravity(Gravity.CENTER_VERTICAL);
        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.QJRadioButton, defStyleAttr, 0);
        String title = a.getString(R.styleable.QJRadioButton_qjrb_title);
        isSel = a.getBoolean(R.styleable.QJRadioButton_qjrb_checked, false);
        qjrb_enabled=a.getBoolean(R.styleable.QJRadioButton_qjrb_enabled,true);
        a.recycle();
        iv = getImg();
        iv.setImageResource(isSel ? R.drawable.ic_radio_sel : R.drawable.ic_radio_nor);
        addView(iv);
        tv = getTxt();
        if (!StringUtils.isEmpty(title)) {
            tv.setText(title);
        }
        addView(tv);
        if(qjrb_enabled){
            setOnClickListener(clickListener);
        }

    }

    @Override
    public void setEnabled(boolean enabled) {
        super.setEnabled(enabled);
        this.qjrb_enabled=enabled;
        setOnClickListener(enabled?clickListener:null);
    }

    View.OnClickListener clickListener=new OnClickListener() {
        @Override
        public void onClick(View view) {
            if(isSel){
                return;
            }
            isSel=true;
            iv.setImageResource( R.drawable.ic_radio_sel);

            if(onCheckedChangeListener!=null){
                onCheckedChangeListener.onCheckedChanged(QJRadioButton.this,isSel);
            }
        }
    };
    @Override
    public void setOnCheckedChangeListener(OnCheckedChangeListener onCheckedChangeListener) {
        this.onCheckedChangeListener = onCheckedChangeListener;
    }
    @Override
    public boolean isChecked(){
        return isSel;
    }
    @Override
    public void setCheck(boolean isCheck){
        if(isSel==isCheck){
            return;
        }
        isSel=isCheck;
        iv.setImageResource(isSel ? R.drawable.ic_radio_sel : R.drawable.ic_radio_nor);
        if(onCheckedChangeListener!=null){
            onCheckedChangeListener.onCheckedChanged(QJRadioButton.this,isSel);
        }
    }
    private ImageView getImg() {
        ImageView iv = new ImageView(getContext());
        iv.setLayoutParams(new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
        return iv;
    }

    private TextView getTxt() {
        TextView tv = new TextView(getContext());
        LayoutParams params = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        params.leftMargin = SizeUtils.dp2px(10);
        tv.setLayoutParams(params);
        tv.setSingleLine();
        tv.setTextSize(TypedValue.COMPLEX_UNIT_SP, 14);
        tv.setTextColor(0xff1C2B33);
        return tv;
    }
}
